package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustException;
import com.heima.common.exception.CustomException;
import com.heima.feigns.ArticleFeign;
import com.heima.feigns.WemediaFeign;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.common.constants.admin.AdminConstants;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDTO;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.model.wemedia.pojos.ApUser;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.ApUserRealnameService;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {
    @Autowired
    private ApUserMapper apUserMapper;
    @Autowired
    WemediaFeign wemediaFeign;
    @Autowired
    private ArticleFeign articleFeign;

    /**
     * 查询列表
     *
     * @param authDTO
     * @return
     */

    @Override
    public ResponseResult apiLoginByStatus(AuthDTO authDTO) {
        if (authDTO == null) {
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        authDTO.checkParam();
        Page<ApUserRealname> page = new Page<>(authDTO.getPage(), authDTO.getSize());
        LambdaQueryWrapper<ApUserRealname> queryWrapper = Wrappers.lambdaQuery();
        if (authDTO.getStatus() != null) {
            queryWrapper.eq(ApUserRealname::getStatus, authDTO.getStatus());
        }
        IPage<ApUserRealname> result = page(page, queryWrapper);
        return new PageResponseResult(authDTO.getPage(), authDTO.getSize(), result.getTotal(), result.getRecords());
    }
    @GlobalTransactional(rollbackFor = Exception.class)
    //@Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseResult authPassandFail(AuthDTO dto, Short status) {
        if (dto.getId() == null) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUserRealname realname = getById(dto.getId());

        if (realname == null) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW, "数据不存在");
        }

        if (!AdminConstants.WAIT_AUTH.equals(realname.getStatus())) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW, "实名认证信息非待审核状态");
        }
        ApUser apUser = apUserMapper.selectById(realname.getUserId());
        if (apUser == null) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST, "关联用户信息不存在");
        }
        realname.setStatus(status);
        if (StringUtils.isNotBlank(dto.getMsg())) {
            realname.setReason(dto.getMsg());
        }
        updateById(realname);
        if (AdminConstants.PASS_AUTH.equals(status)) {
            WmUser wmUser = createWmUser(dto, apUser);
            createAuthor(wmUser);
        }


        return ResponseResult.okResult();
    }

    private void createAuthor(WmUser wmUser) {
        ResponseResult<ApAuthor> authorResult = articleFeign.selectById(wmUser.getApUserId());
        if (authorResult.getCode().intValue()!=0){
            CustException.cust(AppHttpCodeEnum.SERVER_ERROR,authorResult.getErrorMessage());
        }
        ApAuthor apAuthor = authorResult.getData();
        if (apAuthor!=null){
            CustException.cust(AppHttpCodeEnum.DATA_EXIST,"作者信息已存在");
        }
        apAuthor=new ApAuthor();
        apAuthor.setUserId(wmUser.getApUserId());
        apAuthor.setCreatedTime(new Date());
        apAuthor.setName(wmUser.getName());
        apAuthor.setWmUserId(wmUser.getId());
        apAuthor.setType(wmUser.getType());
        ResponseResult<ApAuthor> apAuthorResult = articleFeign.insertWmUser(apAuthor);
        if (apAuthorResult.getCode()!=0){
            CustException.cust(AppHttpCodeEnum.SERVER_ERROR,apAuthorResult.getErrorMessage());
        }

    }


    private WmUser createWmUser(AuthDTO dto, ApUser apUser) {
        ResponseResult<WmUser> wmUserResult = wemediaFeign.selectByName(apUser.getName());
        if (wmUserResult.getCode().intValue() != 0) {
            CustException.cust(AppHttpCodeEnum.SERVER_ERROR, wmUserResult.getErrorMessage());
        }
        WmUser wmUser = wmUserResult.getData();
        if (wmUser != null) {
            CustException.cust(AppHttpCodeEnum.DATA_EXIST, "自媒体数据已存在");
        }
        wmUser = new WmUser();
        wmUser.setApUserId(apUser.getId());
        wmUser.setName(apUser.getName());
        wmUser.setPassword(apUser.getPassword());
        wmUser.setSalt(apUser.getSalt());
        wmUser.setImage(apUser.getImage());
        wmUser.setPhone(apUser.getPhone());
        wmUser.setStatus(AdminConstants.PASS_AUTH.intValue());
        wmUser.setType(0);
        wmUser.setCreatedTime(new Date());
        ResponseResult<WmUser> insertResult = wemediaFeign.insertWmUser(wmUser);
        if (insertResult.getCode().intValue() != 0) {
            CustException.cust(AppHttpCodeEnum.SERVER_ERROR, insertResult.getErrorMessage());
        }
        return insertResult.getData();
    }


}